<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    protected $fillable = ['order_sn', 'uid', 'price', 'goods_id', 'status','sale_id'];


    public static function boot()
    {
        parent::boot();
        self::creating(function (Order $order){
            $order->status = 1;
            $order->uid = \Auth::id();
            $order->order_sn = 'sn_'.Carbon::today().rand(10000,99999);
        });
        self::created(function (Order $order){
            Goods::where('id',$order->goods_id)->update(['status'=>2]);
        });
    }

    public function goods(){
        return $this->hasOne(Goods::class,"id","goods_id");
    }
}
